package com.mazaiting.instructions.conversions;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * l2x系列指令把long变量强制转换成其他类型
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class L2xInstruction {

    /**
     * 长整型转换为双精度
     */
    public static class L2D extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            double v2 = (double) v1;
            stack.pushDouble(v2);
        }
    }

    /**
     * 长整型转换为单精度
     */
    public static class L2F extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            float v2 = (float) v1;
            stack.pushFloat(v2);
        }
    }

    /**
     * 长整型转换为整型
     */
    public static class L2I extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            int v2 = (int) v1;
            stack.pushInt(v2);
        }
    }

}
